package com.alstudio.utils.chat;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.alstudio.module.xmpp.protocol.xmpp.packact.ALMessage;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.loovee.imaohu.R;

/**
 * 用于生成聊天布局，管理聊天信息
 * 
 * @author alonso lee
 * 
 */
public class ALChatViewManager implements OnClickListener,
		ALChatViewManagerEngin {

	// 加入聊天管理器的父布局
	private LinearLayout mParentContainer;
	private LayoutInflater mInflater;
	// 下拉刷新的scrolview
	private PullToRefreshScrollView mContainer;
	// scrollview
	private ScrollView mScrollView;
	// 加入聊天管理器的界面
	private Activity mActivity;
	private ChatMsgUtil mChatMsgUtil;
	// 无数据时的提示view
	private View emptyNoticeView;
	// 聊天管理器工作状态监听器
	private ALChatViewManagerWorkingStateListener mWorkingStateListener;

	/**
	 * 构造聊天管理器，缺省不支持下拉刷新
	 * 
	 * @param inflater
	 * @param parent
	 * @param activty
	 */
	public ALChatViewManager(LayoutInflater inflater, LinearLayout parent,
			Activity activty) {
		// TODO Auto-generated constructor stub
		this.mParentContainer = parent;
		this.mInflater = inflater;
		this.mActivity = activty;
		initManager();
	}

	/**
	 * 初始化聊天管理器
	 */
	private void initManager() {
		mContainer = (PullToRefreshScrollView) mInflater.inflate(
				R.layout.base_chat_layout, null);

		mScrollView = mContainer.getRefreshableView();

		mChatMsgUtil = new ChatMsgUtil(mActivity, mScrollView, this);

		mParentContainer.addView(mContainer,LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);

		setPullRefreshEnable(false);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showChatMessage(ALMessage msg) {
		// TODO Auto-generated method stub
		mChatMsgUtil.setMessage(msg);
		if (mWorkingStateListener != null) {
			mWorkingStateListener.onViewAdd();
		}
	}

	@Override
	public void removeChatMessage(ALMessage msg) {
		// TODO Auto-generated method stub
		if (mWorkingStateListener != null) {
			mWorkingStateListener.onViewDelete();
		}

	}

	@Override
	public void clearAllChatMessage() {
		// TODO Auto-generated method stub
		mChatMsgUtil.clear();
		if (mWorkingStateListener != null) {
			mWorkingStateListener.onAllViewDelete();
		}

	}

	@Override
	public void release() {
		// TODO Auto-generated method stub
		clearAllChatMessage();
		mParentContainer.removeAllViews();
		mWorkingStateListener = null;
		mParentContainer = null;
		mChatMsgUtil = null;
		mActivity = null;
	}

	@Override
	public void setEmptyNoticeView(View emptyNoticeView) {
		// TODO Auto-generated method stub
		if (emptyNoticeView != null) {
			this.emptyNoticeView = emptyNoticeView;
		}
	}

	@Override
	public void setALChatViewWorkingStateListener(
			ALChatViewManagerWorkingStateListener listener) {
		// TODO Auto-generated method stub
		mWorkingStateListener = listener;
	}

	@Override
	public void setPullRefreshEnable(boolean enable) {
		// TODO Auto-generated method stub
		if (enable) {
			// 开启下拉刷新
			mContainer.setMode(Mode.PULL_FROM_START);
		} else {
			// 禁止下拉刷新
			mContainer.setMode(Mode.DISABLED);
		}

	}
}
